<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
<!--        <el-form-item label="旅行社名称" prop="deptId">-->
<!--          <el-tree-select-->
<!--              v-model="queryParams.deptId"-->
<!--              placeholder="全部"-->
<!--              :data="deptOptions"-->
<!--              filterable-->
<!--              clearable-->
<!--              check-strictly-->
<!--              :render-after-expand="false"-->
<!--              style="width: 200px"-->
<!--          />-->
<!--        </el-form-item>-->
        <el-form-item label="关键字" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="分类名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon>
              <Search />
            </template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon>
              <Refresh />
            </template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <div class="mb-10px">
        <el-button
          v-hasPerm="['system:spotCategory:add']"
          type="success"
          @click="handleOpenDialog()"
        >
          <template #icon>
            <Plus />
          </template>
          新增
        </el-button>
        <el-button
          v-hasPerm="['system:spotCategory:delete']"
          type="danger"
          :disabled="removeIds.length === 0"
          @click="handleDelete()"
        >
          <template #icon>
            <Delete />
          </template>
          删除
        </el-button>
      </div>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column type="index" label="序号" width="80" align="center" />
<!--        <el-table-column key="name" label="旅行社名称" prop="deptName" min-width="150" align="center" />-->
        <el-table-column key="name" label="分类名称" prop="name" min-width="150" align="center" />
        <el-table-column key="remark" label="备注" prop="remark" min-width="150" align="center" />
        <el-table-column fixed="right" label="操作" width="150" align="center">
          <template #default="scope">
            <el-button
              v-hasPerm="['system:spotCategory:edit']"
              type="primary"
              size="small"
              link
              @click="handleOpenDialog(scope.row.id)"
            >
              <template #icon>
                <Edit />
              </template>
              编辑
            </el-button>
            <el-button
                v-hasPerm="['system:spotCategory:delete']"
                type="danger"
                size="small"
                link
                @click="handleDelete(scope.row.id)"
            >
              <template #icon>
                <Delete />
              </template>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery()"
      />
    </el-card>

    <!-- 预售票分类表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="500px"
      @close="handleCloseDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
<!--        <el-form-item label="旅行社名称" prop="deptId">-->
<!--          <el-tree-select-->
<!--              v-model="formData.deptId"-->
<!--              placeholder="请选择旅行社名称"-->
<!--              :data="deptOptions"-->
<!--              filterable-->
<!--              check-strictly-->
<!--              :render-after-expand="false"-->
<!--              style="width: 100%"-->
<!--          />-->
<!--        </el-form-item>-->
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="formData.name" placeholder="分类名称" maxlength="64" show-word-limit/>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="formData.remark" placeholder="备注" maxlength="512" show-word-limit rows="3"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :disabled="loading" @click="handleSubmit()">确定</el-button>
          <el-button @click="handleCloseDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import DeptAPI from "@/api/system/dept";

defineOptions({
  name: "SpotCategory",
  inheritAttrs: false,
});

import SpotCategoryAPI, {
  SpotCategoryPageVO,
  SpotCategoryForm,
  SpotCategoryPageQuery,
} from "@/api/system/spot-category";

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const loading = ref(false);
const removeIds = ref<number[]>([]);
const total = ref(0);
// 部门下拉数据源
const deptOptions = ref<OptionType[]>();
const queryParams = reactive<SpotCategoryPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 预售票分类表格数据
const pageData = ref<SpotCategoryPageVO[]>([]);

// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

// 预售票分类表单数据
const formData = reactive<SpotCategoryForm>({});

// 预售票分类表单校验规则
const rules = reactive({
  name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
  deptId: [{ required: true, message: "请选择旅行社名称", trigger: "blur" }],
});

/** 查询预售票分类 */
function handleQuery() {
  loading.value = true;
  SpotCategoryAPI.getPage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置预售票分类查询 */
function handleResetQuery() {
  queryFormRef.value!.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 打开预售票分类弹窗 */
async function handleOpenDialog(id?: number) {
  dialog.visible = true;
  // 加载部门下拉数据源
  deptOptions.value = await DeptAPI.getOptions();
  if (id) {
    dialog.title = "修改预售票分类";
    SpotCategoryAPI.getFormData(id).then((data) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增预售票分类";
  }
}

/** 提交预售票分类表单 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        SpotCategoryAPI.update(id, formData)
          .then(() => {
            ElMessage.success("修改成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        SpotCategoryAPI.add(formData)
          .then(() => {
            ElMessage.success("新增成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭预售票分类弹窗 */
function handleCloseDialog() {
  dialog.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}

/** 删除预售票分类 */
function handleDelete(id?: number) {
  const ids = [id || removeIds.value].join(",");
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      loading.value = true;
      SpotCategoryAPI.deleteByIds(ids)
        .then(() => {
          ElMessage.success("删除成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    () => {
      ElMessage.info("已取消删除");
    }
  );
}
if (onMounted) {
  onMounted(async () => {
    // 加载区域下拉数据源
    deptOptions.value = await DeptAPI.getOptions();
    handleQuery();
  });
}
</script>
